package cn.ekuma.epos.productsfinishing;

import java.util.List;
import java.util.Properties;


import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;

import cn.ekuma.data.ui.swing.AbstractChildDAOJEditor;
import cn.ekuma.epos.datalogic.I_DataLogicSales;

import com.openbravo.bean.Location;
import com.openbravo.bean.ProductStockLevel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import java.awt.Dimension;

public class ProductStockLevelEditor extends AbstractChildDAOJEditor<ProductStockLevel,String> { 
	
	I_DataLogicSales dlSales;
	
	private ComboBoxValModel m_LocationsModel;

	private JComboBox m_jLocation;
	
	public ProductStockLevelEditor(I_DataLogicSales dlSales,DirtyManager dirty) {
		super(dirty);
		setPreferredSize(new Dimension(380, 120));
		this.dlSales=dlSales;
		initComponents();
		m_jMinimum.getDocument().addDocumentListener(dirty);
        m_jMaximum.getDocument().addDocumentListener(dirty);
        m_jLocation.addActionListener(dirty);
	}

	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel4 = new javax.swing.JLabel();
        m_jMinimum = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        m_jMaximum = new javax.swing.JTextField();

        setLayout(null);

        jLabel4.setText(AppLocal.getIntString("label.minimum")); // NOI18N
        add(jLabel4);
        jLabel4.setBounds(10, 52, 110, 15);

        m_jMinimum.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        add(m_jMinimum);
        m_jMinimum.setBounds(124, 52, 80, 21);

        jLabel5.setText(AppLocal.getIntString("label.maximum")); // NOI18N
        add(jLabel5);
        jLabel5.setBounds(10, 82, 110, 15);

        m_jMaximum.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        add(m_jMaximum);
        m_jMaximum.setBounds(124, 82, 80, 21);
    	
    	JLabel label = new JLabel();
    	label.setText(AppLocal.getIntString("label.warehouse"));
    	label.setBounds(10, 27, 110, 15);
    	add(label);
    	
    	 m_jLocation = new JComboBox();
    	 m_jLocation.setBounds(124, 24, 220, 20);
    	add(m_jLocation);
    }// </editor-fold>//GEN-END:initComponents
	
	@Override
	public String getTabName() {
		return AppLocal.getIntString("label.prodstocklevel");
	}
	
	@Override
	public void refresh() {
	}
	@Override
	public ProductStockLevel createNew(Properties prop) {
		ProductStockLevel obj=new ProductStockLevel();
		obj.setProductId(parentNode.getParentKey().getKey());
		return obj;
	}
	@Override
	public void rebuild(ProductStockLevel obj) throws BasicException {
		obj.setStockSecurity((Double) Formats.DOUBLE.parseValue(m_jMinimum.getText()));
        obj.setStockMax((Double) Formats.DOUBLE.parseValue(m_jMaximum.getText()));
        obj.setLocationId((String) m_LocationsModel.getSelectedKey());
        obj.setLocation((Location) m_LocationsModel.getSelectedItem());
	}
	@Override
	public void updateUI(boolean editAble, int state) {
        m_jMinimum.setText(Formats.DOUBLE.formatValue(obj.getStockSecurity()));
        m_jMaximum.setText(Formats.DOUBLE.formatValue(obj.getStockMax()));
        if(obj.getLocationId()!=null)
           m_LocationsModel.setSelectedKey(obj.getLocationId());
	}

	@Override
	public void activate() throws BasicException {
		List a =dlSales.list(Location.class);
        m_LocationsModel = new ComboBoxValModel(a);
        m_LocationsModel.setSelectedFirst();
        m_jLocation.setModel(m_LocationsModel); // refresh model
	}

	@Override
	public Class getEditClass() {
		return ProductStockLevel.class;
	}
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField m_jMaximum;
    private javax.swing.JTextField m_jMinimum;
}
